package com.cn.evaluate.handler;

import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.dom4j.Element;

/**
 * 截取XML文件
 * 
 * @author admin
 * 
 */
public class XMLHandler extends DefaultHandler {
	/**
	 * 日志管理
	 */
	public static Logger logger = Logger.getLogger(XMLHandler.class);

	/**
	 * 获取xml文件全路径
	 */
	public XMLHandler(String xmlFilePath) {
		super(xmlFilePath);
	}

	/**
	 * 使用示例：给出年级、性别，即可取得指定项目的评测标准
	 * 
	 * @param args
	 * @throws DocumentException
	 */
	public static void main(String[] args) throws DocumentException {
		XMLHandler handler = new XMLHandler(
				"src/com/cn/evaluate/resource/height_weight.xml");
		String xpath = "/subject/rulers/ruler[@grade='1' and @sex='0']";
		Element root = handler.getRootElement();
		Element result = handler.getElementXML(root, xpath);
		logger.debug(result.asXML());
	}

	/**
	 * 获取xml文件片段
	 * 
	 * @param element
	 * @param xpath
	 * @return xml元素
	 */
	public Element getElementXML(Element element, String xpath) {
		List<?> result = super.getElement(element, xpath);
		return (Element) result.get(0);
	}

	/**
	 * 获取xml文件全路径
	 */
	public String getXmlFilePath() {
		return xmlFilePath;
	}

	/**
	 * 获取xml文件全路径
	 */
	public void setXmlFilePath(String xmlFilePath) {
		this.xmlFilePath = xmlFilePath;
	}
}
